package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //调用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //条件查询权限数据
        List<Permission> permissionList = permissionMapper.findByCondition(queryPageBean.getQueryString());
        PageInfo<Permission> pageInfo = new PageInfo<>(permissionList);

        //将分页数据存入pageResult中
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void deleteById(Integer id) {

        //通过id查询是否存在中间表关系
        Integer count = permissionMapper.findCountByPermissionId(id);
        //判断当前权限是否被角色引用过
        if(count > 0){
            //抛出异常
            throw new RuntimeException("当前权限被引用,无法删除");
        }
        //删除
        permissionMapper.deleteById(id);

    }

    @Override
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }

}
